package org.example.common;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Aspect
@Component
public class LogConfig {

    @Pointcut("execution( * org.example.controller.*.*(..))")
    public void pointCut(){

    }

    @Around("pointCut()")
    @SneakyThrows
    public Object aspectLog(ProceedingJoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        //获取url
        String uri = request.getRequestURI();

        //ip
        String ip = request.getRemoteAddr();

        //请求方式
        String requestMethod = request.getMethod();

        //类名
        String className = joinPoint.getTarget().getClass().getName();

        //方法名
        String method = joinPoint.getSignature().getName();

        //参数列表
        Object[] args = joinPoint.getArgs();

        //放行
        Object obj = joinPoint.proceed();

        log.info("ip:{},url:{},type:{},targs:{},result:{}",ip,uri,requestMethod,args,obj);

        return obj;
    }





}


